package gov.kqqzyyy.monkey.ydhl.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.ArrayMap;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.Collections;
import java.util.List;

import adapter.PatientsListAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import gov.kqqzyyy.monkey.ydhl.R;
import mytools.MyBaseActivity;
import mytools.MyServiceUtil;
import objects.InPatient;
import objects.Section;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PatientListActivity extends MyBaseActivity implements SwipeRefreshLayout.OnRefreshListener, ListView.OnItemClickListener {

    @BindView(R.id.which_area)
    TextView sectionName;
    @BindView(R.id.patient_swipe_ly)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.patients_list)
    ListView listView;

    private PatientsListAdapter patientsAdapter;
    private List<InPatient> patientList;

    private Call<String> patientCall;

    private Section chooseSection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_list);
        ButterKnife.bind(this);

        patientsAdapter = new PatientsListAdapter(this);
        listView.setAdapter(patientsAdapter);
        listView.setOnItemClickListener(this);

        swipeRefreshLayout.setColorSchemeResources(R.color.holo_green_dark, R.color.holo_green_light, R.color.holo_orange_light, R.color.holo_red_light);
        swipeRefreshLayout.setOnRefreshListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        chooseSection = getMyApplication().getCurrentSection();
        if (chooseSection != null) {
            doGetPatients(true, chooseSection.getKSDM());
        } else {
            Toast.makeText(getApplicationContext(), "获取科室参数失败！", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onStop() {
        super.onStop();
        if (patientCall != null && patientCall.isExecuted()) {
            patientCall.cancel();
        }
    }

    @Override
    public void onRefresh() {
        doGetPatients(false, chooseSection.getKSDM());
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        InPatient inPatient = patientList.get(position);
        if (inPatient != null) {
            getMyApplication().setChoosePatient(inPatient);
            Intent intent = new Intent(getApplicationContext(), PatientDetailActivity.class);
            startActivity(intent);
        }
    }

    //获取病区病人
    private void doGetPatients(boolean showProgressBar, String brbq) {
        if (showProgressBar) {
            showProgressDialog("获取病人数据中");
        }
        patientCall = MyServiceUtil.getPatientCall(brbq);
        Callback<String> callback = new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                cancelProgressDialog();
                stopRefreshing();
                String result = response.body();
                if (result != null) {

                    Gson gson = new Gson();
                    patientList = gson.fromJson(result, new TypeToken<List<InPatient>>() {
                    }.getType());
                    if (patientList != null && patientList.size() > 0) {
                        //按床位号排序
                        Collections.sort(patientList, (arg0, arg1) -> arg0.getBRCH().compareTo(arg1.getBRCH()));
                        patientsAdapter.setList(patientList);
                        ArrayMap<Section, List<InPatient>> dataSet = getMyApplication().getmDataMap();
                        if (dataSet == null) {
                            dataSet = new ArrayMap<>();
                        }
                        dataSet.put(chooseSection, patientList);
                        getMyApplication().setmDataMap(dataSet);
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                cancelProgressDialog();
                stopRefreshing();
                Toast.makeText(getApplicationContext(), "获取病人数据失败!", Toast.LENGTH_SHORT).show();
            }
        };
        patientCall.enqueue(callback);

    }
}
